package net.maku.member.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.framework.operatelog.annotations.OperateLog;
import net.maku.framework.operatelog.enums.OperateTypeEnum;
import net.maku.member.convert.MemberUserConvert;
import net.maku.member.entity.MemberUserEntity;
import net.maku.member.service.MemberUserService;
import net.maku.member.query.MemberUserQuery;
import net.maku.member.vo.MemberUserTransferVO;
import net.maku.member.vo.MemberUserVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 会员信息
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-15
 */
@RestController
@RequestMapping("/member/user")
@Tag(name = "会员信息")
@AllArgsConstructor
public class MemberUserController {
    private final MemberUserService memberUserService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('member:user:page')")
    public Result<PageResult<MemberUserVO>> page(@ParameterObject @Valid MemberUserQuery query) {
        PageResult<MemberUserVO> page = memberUserService.page(query);

        return Result.ok(page);
    }

    @GetMapping("page/init")
    @Operation(summary = "入会申请分页")
    @PreAuthorize("hasAuthority('member:user:init')")
    public Result<PageResult<MemberUserVO>> pageInit(@ParameterObject @Valid MemberUserQuery query) {
        query.setAuditStatus(0);
        PageResult<MemberUserVO> page = memberUserService.pageByConditions(query);
        return Result.ok(page);
    }

    @GetMapping("page/remove")
    @Operation(summary = "移除会员分页")
    @PreAuthorize("hasAuthority('member:user:remove')")
    public Result<PageResult<MemberUserVO>> pageRemove(@ParameterObject @Valid MemberUserQuery query) {
        query.setRemoveStatus(1);
        PageResult<MemberUserVO> page = memberUserService.pageByConditions(query);
        return Result.ok(page);
    }

    @GetMapping("page/transfer")
    @Operation(summary = "会员转会申请分页")
    @PreAuthorize("hasAuthority('member:user:transfer')")
    public Result<PageResult<MemberUserTransferVO>> pageTransfer(@ParameterObject @Valid MemberUserQuery query) {
        PageResult<MemberUserTransferVO> page = memberUserService.pageTransfer(query);
        return Result.ok(page);
    }

    @GetMapping("shift/in/{id}")
    @Operation(summary = "移入工会")
    @PreAuthorize("hasAuthority('member:user:in')")
    public Result<MemberUserVO> shiftIn(@PathVariable("id") Long id) {
        memberUserService.shiftIn(id);
        return Result.ok();
    }

    @GetMapping("shift/out/{id}")
    @Operation(summary = "移出工会")
    @PreAuthorize("hasAuthority('member:user:out')")
    public Result<MemberUserVO> shiftOut(@PathVariable("id") Long id) {
        memberUserService.shiftOut(id);
        return Result.ok();
    }


    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('member:user:info')")
    public Result<MemberUserVO> get(@PathVariable("id") Long id) {

        return Result.ok(memberUserService.getInfo(id));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('member:user:save')")
    public Result<String> save(@RequestBody MemberUserVO vo) {
        memberUserService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('member:user:update')")
    public Result<String> update(@RequestBody @Valid MemberUserVO vo) {
        memberUserService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('member:user:delete')")
    public Result<String> delete(@RequestBody List<Long> idList) {
        memberUserService.delete(idList);

        return Result.ok();
    }


    @PostMapping("import")
    @Operation(summary = "导入会员")
    @OperateLog(type = OperateTypeEnum.IMPORT)
    @PreAuthorize("hasAuthority('member:user:import')")
    public Result<String> importExcel(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("请选择需要上传的文件");
        }
        memberUserService.importByExcel(file);
        return Result.ok();
    }

    @GetMapping("export")
    @Operation(summary = "导出会员")
    @OperateLog(type = OperateTypeEnum.EXPORT)
    @PreAuthorize("hasAuthority('member:user:export')")
    public void export(@RequestParam("orgId") String orgId) {
        memberUserService.export(orgId);
    }


}